import React from "react";
import Divider from "antd/lib/divider";
import Timeline from "antd/lib/timeline";
import Button from "antd/lib/button";
import EmployeeActivity from "@/services/employeeactivity";
import CreateFollowUpDialog from "@/components/employee/FollowUpDialog";
import { currentUser } from "@/services/auth";

export default class EmployeeFollowUp extends React.Component {
  state = {
    loading: true,
    data: [],//跟进列表
  };
  componentWillMount() {
    this.refreshData(this.props.id);
  }
  refreshData(id) {
    EmployeeActivity.getbyemployeeid({ employeeid: id, sourcetype: this.props.sourcetype }).then(data => {
      if (data.length > 0) {
        this.setState({
          data: data,
          loading: false,
        });
      } else {
        this.setState({
          loading: false,
        });
      }
    }).catch(error => {
      this.setState({
        loading: false,
      });
    });
  }
  fieldedit(field) {
    if (!this.props.isedit) {
      return true;
    }
    if (this.props.colcontrol) {
      if (this.props.allow_col.indexOf(field) >= 0) {
        return false;
      } else {
        return true;
      }
    } else {
      return false;
    }
  }
  handleedit = (employeeactivityid) => {
    CreateFollowUpDialog.showModal({ employeeactivityid: employeeactivityid, employeeid: this.props.id,sourcetype: this.props.sourcetype })
      .onClose((result = {}) => {
        if (result.success) {
          this.refreshData(this.props.id);
        }
      }).onDismiss(() => {
      });
  };
  pushbill = (items) => {
    return (<Timeline.Item dot={<i className="fa fa-home m-r-5" />} color="blue">
      {currentUser.id === items.userid && (<Button type="primary" onClick={() => this.handleedit(items.id)}>
        <i className="fa fa-plus m-r-5" />
        修改
      </Button>)}
      <b>日期：{items.created_ed}</b><b>&nbsp;&nbsp;&nbsp;跟进人：{items.username}</b> <b>&nbsp;&nbsp;&nbsp;跟进内容：</b><br />
      <div dangerouslySetInnerHTML={{ __html: items.message }}></div>
    </Timeline.Item>);
  };
  render() {
    if (this.state.loading) return null;
    return (
      <div>
        <div style={{ marginBottom: 16 }}>
          <Divider type="vertical" />
          <Button type="primary" style={{ margin: "10px" }} onClick={() => this.handleedit(0)} >
            <i className="fa fa-plus m-r-5" />
            新增跟进记录
          </Button>
        </div>
        <div>
          <Divider type="horizontal" />
          <Timeline style={{ background: '#fff' }}>
            {(this.state.data || []).map(item => (
              this.pushbill(item)
            ))}
          </Timeline>
        </div>
      </div>
    );
  }
}
